/**
 *  Module
 *
 * Description
 */
angular
  .module('sfhtHaitaoCmsApp')
  .controller('ModuleEditCtrl', function($scope, $modal, $injector, $routeParams, $location) {
    $scope.init = function(){
      var commonBizService = $injector.get("CommonBiz");

      /**
       * 获取所有的子模块
       * @param  {int} 模块id
       * @return {array}
       */
      var subModuleList = commonBizService.getSubModuleList($routeParams.id)
          .then(function(data) {
            $scope.subModuleList = data.value;
          });
        var getFields = commonBizService.getAllFields()
          .then(function(data) {
            $scope.getAllFields = data.value;
          });
    };
    var empty = function(){
    };
    //模块属性
    $scope.submoduleEdit = function(subModule, $event, isEdit) {
       $event.preventDefault();
      var modal = $modal.open({
        animation: true,
        templateUrl: 'modal.submodule.info.html',
        controller: 'ModalEditSubModuleCtrl',
        size: 'lg',
        resolve: {
          subModule: function() {
            return subModule;
          },
          isEdit: function(){
            return isEdit;
          },
          allfields: function(){
            return $scope.getAllFields;
          },
          moduleId: function(){
            return $routeParams.id;
          }
        }
      });

      modal.result.then(empty, empty);
    };
  })
.controller('ModalEditSubModuleCtrl', function($scope, $route, $modal, $injector, $modalInstance, subModule, isEdit, allfields, moduleId){
     var commonBizService = $injector.get("CommonBiz");
     $scope.subModule = subModule || {'cmsDataProcessor':[]};
     $scope.isEdit = isEdit;
      if (!$scope.isEdit) {
        $scope.subModule.autoSync = 0;
      }
    console.log($scope.subModule.processorNames);
     $scope.allfields = allfields;
     _.each(allfields, function(field){
      field.checked = false;
      if(typeof $scope.subModule.showFields != "undefined" && $scope.subModule.showFields.length > 0){
        _.each($scope.subModule.showFields, function(value){
          if(field.fieldName == value.fieldName){
            field.checked = true;
          }
        });
      }
    });
     $scope.init = function(){
        /**
         * 获取所有的处理器列表
         * @param
         * @return {array}
         */
        var processListValue = commonBizService.getDataProcessorList()
          .then(function(data) {
            $scope.processList = data.value;
            $scope.temArray = [];
            var tem = [];
            _.each($scope.subModule.cmsDataProcessor,function(value){
              tem.push(value.beanName);
              _.each($scope.processList,function(process,index){
                if(value.beanName == process.beanName){
                  $scope.temArray[index] = true;
                }
              });
            });

            console.log(tem);
            $scope.subModule.cmsDataProcessor = tem;
          });
     }

     $scope.changeValue = function(field){
       field.checked = !field.checked;
     };

     $scope.checkValue = function(field, showFields){
        _.each(showFields, function(value){
          if(value.fieldName == field.fieldName){
              value.checked = true;
          }
        })
     };

     function getShowFields(allfields, subModule){
        var showFields = [];
        var i = 0;
        _.each(allfields, function(field){
          if(field.checked){
            showFields[i++] = field;
          }
        });
        subModule.showFields = showFields;
     };

     /**
       * 子模块的修改操作
       * @param {Object} 子模块的信息
       * @return {array}
       */
     $scope.save = function(subModule){
       var tem = [];
       _.each(subModule.cmsDataProcessor,function(value){
         tem.push({"beanName":value});
       });
       console.log(tem);
       subModule.cmsDataProcessor = tem;
        getShowFields($scope.allfields, subModule);
        var updateModule = commonBizService.updateSubModule(subModule)
          .then(function(data) {
            alert("编辑成功！！");
            $modalInstance.close(data);
          }, function(data) {
            alert("编辑失败，" + data);
          });
     };

     /**
       * 子模块的新增操作
       * @param {Object} 子模块的信息
       * @return {array}
       */
     $scope.add = function(subModule){
       var tem = [];
       _.each(subModule.cmsDataProcessor,function(value){
         tem.push({"beanName":value});
       });
       console.log(tem);
       subModule.cmsDataProcessor = tem;
       getShowFields($scope.allfields, subModule);
       subModule.moduleId = moduleId;

        var addModule = commonBizService.createSubModule(subModule)
          .then(function(data) {
            if(data.value){
              alert("新增成功！！");
              $modalInstance.close(data);
              $route.reload();
              // window.location.reload();
            }
            else{
              alert("编辑失败，" + data);
            }
          }, function(data) {
            alert("编辑失败，" + data);
          })
     };
     $scope.cancel = function(){
        $modalInstance.dismiss("cancel");
     };
  });
